package com.cmsky.open.base.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.org.rapid_framework.web.scope.Flash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.core.util.PagerUitl;

import cn.org.rapid_framework.beanutils.BeanUtils;

import java.util.*;

import com.cmsky.open.core.base.*;
import com.cmsky.open.core.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.cmsky.open.base.model.*;
import com.cmsky.open.base.dao.*;
import com.cmsky.open.base.manager.*;
import com.cmsky.open.base.vo.query.*;

/**
 * @author jandyfish email:jandyfish(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Controller("admin.base.goodsClassifyInfo")
@RequestMapping("/admin/base/goodsClassifyInfo")
public class GoodsClassifyInfoController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private GoodsClassifyInfoManager goodsClassifyInfoManager;
	
	private final String LIST_ACTION = "redirect:/admin/base/goodsClassifyInfo/list.do";
	
	public GoodsClassifyInfoController() {
	}
	
	/** 
	 * 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写
	 **/
	public void setGoodsClassifyInfoManager(GoodsClassifyInfoManager manager) {
		this.goodsClassifyInfoManager = manager;
	}

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,GoodsClassifyInfoQuery query) {
		BasePager page = this.goodsClassifyInfoManager.findPage(query);
		
		ModelAndView result = new ModelAndView("/admin/base/goodsClassifyInfo/list");
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	/**
	 * 默认管理页
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("/admin/base/goodsClassifyInfo/index");
	}
	/**
	 * ajax搜索
	 * @param request
	 * @param response
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/ajaxList")
	public String ajaxList(HttpServletRequest request,HttpServletResponse response,GoodsClassifyInfoQuery query) {
		BasePager<GoodsClassifyInfo> page = this.goodsClassifyInfoManager.findPage(query);
		if(page.getRows()!=null){
			for(GoodsClassifyInfo classify : page.getRows()){
				if(null!=classify.getPid() && 0 != classify.getPid()){
					classify.setGoodsClassifyInfo(this.goodsClassifyInfoManager.getById(classify.getPid()));
				}
			}
		}
		return JsonUtils.jsonFromObject(PagerUitl.pasreToLigerMap(page));
	}
	/**
	 * 批量更新或新增
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(HttpServletRequest request,HttpServletResponse response){
		String json = request.getParameter("json");
		Map<String,Object> result = new HashMap<String,Object>();
		List<Integer> successId = new LinkedList<Integer>();
		List tempList = JsonUtils.listFromJson(json);
		for(Object obj : tempList){
			GoodsClassifyInfo bean = JsonUtils.objectFromJson(JsonUtils.jsonFromObject(obj),GoodsClassifyInfo.class);
			if(this.goodsClassifyInfoManager.saveOrUpdate(bean) > 0){
				successId.add(bean.getId());
			}
		}
		result.put("sucessList", successId);
		return JsonUtils.jsonFromObject(result);
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping("show")
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		GoodsClassifyInfo goodsClassifyInfo = (GoodsClassifyInfo)goodsClassifyInfoManager.getById(id);
		return new ModelAndView("/admin/base/goodsClassifyInfo/show","goodsClassifyInfo",goodsClassifyInfo);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping("create")
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,GoodsClassifyInfo goodsClassifyInfo) throws Exception {
		return new ModelAndView("/admin/base/goodsClassifyInfo/create","goodsClassifyInfo",goodsClassifyInfo);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping("save")
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,GoodsClassifyInfo goodsClassifyInfo) throws Exception {
		goodsClassifyInfoManager.save(goodsClassifyInfo);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		GoodsClassifyInfo goodsClassifyInfo = (GoodsClassifyInfo)goodsClassifyInfoManager.getById(id);
		return new ModelAndView("/admin/base/goodsClassifyInfo/edit","goodsClassifyInfo",goodsClassifyInfo);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping("update")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		
		GoodsClassifyInfo goodsClassifyInfo = (GoodsClassifyInfo)goodsClassifyInfoManager.getById(id);
		bind(request,goodsClassifyInfo);
		goodsClassifyInfoManager.update(goodsClassifyInfo);
		Flash.current().success(UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping("delete")
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			
			java.lang.Integer id = new java.lang.Integer((String)params.get("id"));
			
			goodsClassifyInfoManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

